#include <QtGui/QCloseEvent>
#include <QDebug>
#include <fusionnotify.h>

#include "mainwindow.h"
#include "ui_mainwindow.h"

#include "icons.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow),
    mpd(new MPDClient())
{
    connect(mpd, SIGNAL(connectionBroken()), this, SLOT(connectionBroken()));
    connect(mpd, SIGNAL(connected()), this, SLOT(connected()));
    if (!mpd->connection()) connectionBroken();
    ui->setupUi(this);
    setIcons(ui);
    updateUi();
    tick();
    QTimer *timer = new QTimer(this);
    connect(timer, SIGNAL(timeout()), this, SLOT(tick()));
    timer->start(500);
}

void MainWindow::tick()
{
    if(!mpd->checkConnection())
    {
        mpd->connection();
    }
    MPDClient::status *s = mpd->getStatus();
    updateUi(s);
    ui->positionLabel->setText(QString::number(mpd->position(s)));
    ui->horizontalSlider->setMaximum(mpd->totalTime(s));
    ui->horizontalSlider->setValue(mpd->position(s));
    mpd->freeStatus(s);
}

void MainWindow::updateUi(MPDClient::status *s)
{
    if(mpd->isPlaying(s))
    {
        setIconsPause(ui);
        ui->progressWidget->show();
        setLabels(mpd->currentSong());
    }
    else if (mpd->isStoped(s))
    {
        setIconsPlay(ui);
        ui->progressWidget->hide();
        MPDClient::song s;
        s.duration = -1;
        setLabels(s);
    }
    else
    {
        setIconsPlay(ui);
        ui->progressWidget->show();
        setLabels(mpd->currentSong());
    }
}

void MainWindow::setLabels(MPDClient::song s)
{
    if (s.duration != -1)
    {
        ui->artistLabel->setText(QString(tr("<i>by</i> %1").arg(s.artist)));
        ui->titleLabel->setText(QString(tr("%1 <i>from</i> %2").arg(s.title,s.album)));
        ui->durationLabel->setText(QString::number(s.duration));
    }
    else
    {
        ui->artistLabel->setText("");
        ui->titleLabel->setText("");
        ui->durationLabel->setText("");
    }
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::closeEvent(QCloseEvent *event)
{
    // Clean-up
    event->accept();
}


void MainWindow::connectionBroken()
{
    QStringList d;
    d << "BLAH" << "CLOSE MEEEEEE";
    Notifier.notify("FusionBox", "path", "No connection", 3*1000,0,QString(),d);
}

void MainWindow::connected()
{
    QStringList d;
    d << "BLAH" << "CLOSE MEEEEEE";
    Notifier.notify("FusionBox", "path", "Connected", 3*1000,0,QString(),d);
}

void MainWindow::on_nextButton_clicked()
{
    if (mpd->next())
      Notifier.notify("FusionBox", "path", "Next song", 3*1000);
    updateUi();
}

void MainWindow::on_prevButton_clicked()
{
    if (mpd->prev())
      Notifier.notify("FusionBox", "path", "Prev song", 3*1000);
    updateUi();
}

void MainWindow::on_stopButton_clicked()
{
    if (mpd->stop())
        Notifier.notify("FusionBox", "path", "Stop song", 3*1000);
    updateUi();
}

void MainWindow::on_playButton_clicked()
{
    if (mpd->isPlaying())
    {
        if (mpd->pause())
            Notifier.notify("FusionBox", "path", "Pause song", 3*1000);
    }
    else
    {
        if (mpd->play())
            Notifier.notify("FusionBox", "path", "Play song", 3*1000);
    }
    updateUi();
}
